﻿using BrnMall.Core;
using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Services.WJK
{
    public class Staffs
    {
        /// <summary>
        /// 我的所有部门ID
        /// </summary>
        /// <param name="list"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public static string GetMyDepartmentIds(List<StaffDepartment> list, int parentId)
        {
            var ids = new StringBuilder();
            if (parentId == 0)
                list.ForEach(x => ids.AppendFormat(",{0}", x.StaffId));
            else
                ids.AppendFormat(",{0}", parentId);

            return ids.Length > 0 ? ids.ToString().Substring(1) : string.Empty;
        }

        public static string GetStaffIds(List<StaffDepartment> list)
        {
            var ids = new StringBuilder();
            list.ForEach(x => ids.AppendFormat(",{0}", x.StaffId));

            return ids.Length > 0 ? ids.ToString().Substring(1) : string.Empty;
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public static UserInfo CreateUser(int storeId, string nickName)
        {
            var name = DateTime.Now.ToString("yyMMddHHmmssms");

            var userInfo = InitUser(name, nickName, "wjk", storeId);

            userInfo.Uid = Users.CreateUser(userInfo);

            //Users.UpdatePartUser(userInfo);
            return userInfo;
        }


        /// <summary>
        /// 获得有效的用户名
        /// </summary>
        /// <param name="oUserName">开放用户名</param>
        /// <param name="uNamePrefix">用户名前缀</param>
        /// <returns></returns>
        private static string GetValidUserName(string oUserName, string uNamePrefix)
        {
            if (string.IsNullOrWhiteSpace(uNamePrefix))
                uNamePrefix = "bma";

            string validUserName = uNamePrefix + oUserName;
            int length = StringHelper.GetStringLength(validUserName);
            if (length > 20)
                validUserName = validUserName.Substring(0, 20);

            if (!Users.IsExistUserName(validUserName))
                return validUserName;

            validUserName = StringHelper.SubString(validUserName, 14);
            string temp = validUserName + Randoms.CreateRandomValue(6);
            while (Users.IsExistUserName(temp))
            {
                temp = validUserName + Randoms.CreateRandomValue(6);
            }
            return temp;
        }

        /// <summary>
        /// 初始化用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="uNamePrefix">用户名前缀</param>
        /// <param name="regionId">区域id</param>
        /// <returns></returns>
        private static UserInfo InitUser(string userName, string nickName, string uNamePrefix, int storeId)
        {
            UserInfo userInfo = new UserInfo();
            userInfo.Salt = Randoms.CreateRandomValue(6);
            userInfo.Password = Users.CreateUserPassword(Randoms.CreateRandomValue(32, false), userInfo.Salt);
            userInfo.StoreId = 0;//无店铺
            userInfo.MallAGid = 1;//非管理员组
            userInfo.RegisterFromStoreId = storeId; //所属店铺
            userInfo.UserName = GetValidUserName(userName, uNamePrefix);
            userInfo.Email = string.Empty;
            userInfo.Mobile = string.Empty;
            userInfo.NickName = nickName;
            userInfo.Avatar = "";
            userInfo.PayCredits = 0;
            userInfo.RankCredits = 0;
            userInfo.VerifyEmail = 0;
            userInfo.VerifyMobile = 0;
            userInfo.UserRid = 0;
            userInfo.LiftBanTime = new DateTime(1900, 1, 1);
            userInfo.LastVisitTime = DateTime.Now;
            userInfo.LastVisitIP = WebHelper.GetIP();
            userInfo.LastVisitRgId = 0; //地区
            userInfo.RegisterTime = DateTime.Now;
            userInfo.RegisterIP = WebHelper.GetIP();
            userInfo.RegisterRgId = 0;
            userInfo.Gender = 0;
            userInfo.RealName = string.Empty;
            userInfo.Bday = new DateTime(1900, 1, 1);
            userInfo.IdCard = string.Empty;
            userInfo.RegionId = 0;
            userInfo.Address = string.Empty;
            userInfo.Bio = string.Empty;
            
            return userInfo;
        }
    }
}